<template>
  <div class="container">
    <global-header :user="currentUser"></global-header>
    <!-- <h1>{{error.message}}</h1> -->
    <loader v-if="isLoading" :message="error.message"></loader> <!--转圈加载 -->
    <router-view></router-view>
    <footer class="text-center py-4 text-secondary bg-light mt-6">
      <small>
        <ul class="list-inline mb-0">
          <li class="list-inline-item">© 2020 者也专栏</li>
          <li class="list-inline-item">课程</li>
          <li class="list-inline-item">文档</li>
          <li class="list-inline-item">联系</li>
          <li class="list-inline-item">更多</li>
        </ul>
      </small>
    </footer>
  </div>
</template>

<script lang="ts">
import { defineComponent,computed, onMounted,watch} from 'vue'
import {useStore} from 'vuex'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/css/bootstrap.min.css'
import axios from 'axios'
import GlobalHeader from './components/GlobalHeader.vue'
import createMessage from './components/createMessage'
import Loader from './components/Loader.vue'
import {GlobalDataProps} from './store'
export default defineComponent({
  name: 'App',
  components:{
    GlobalHeader,
    Loader,
  },
  setup(){
    const store = useStore<GlobalDataProps>()
    const currentUser = computed(()=>store.state.user)
    const isLoading = computed(()=>store.state.loading)
    const error=computed(()=>store.state.error)
    const token=computed(()=>store.state.token)

    onMounted(()=>{
      if(!store.state.user.isLogin && token.value){ //如果现在用户未登录，但是token有值
        axios.defaults.headers.common.Authorization = `Bearer ${token.value}`
        store.dispatch('fetchCurrentUser')
      }
    })

    watch(()=>error.value.status,()=>{
      const {status,message} = error.value
      if(status && message){  //如果输入的有错误，则错误提示信息存在
        createMessage(message,'error')  //创建message组件
      }
    })
    return {
      currentUser,
      isLoading,
      error
    }
  }
})
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>
